<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<div th:replace="gm/common :: html"></div>

	<div id="addRoleDialog" class="easyui-dialog" title="添加权限请求"
		data-options="iconCls:'icon-save',resizable:true,modal:true,closed:true"
		style="width: 600px; height: 300px; padding: 20px">
		<div align="center">
			<table style="border-collapse: separate; border-spacing: 0px 10px;">
				<tr>
					<td><span id="errorTips" style="color: red"></span></td>
				</tr>
				
				<tr>
					<td>
					<input type="hidden" id="roleId" value=""/>
					角色名称： <input class="easyui-textbox" prompt="角色名称" data-options="editable:false"
						id="name" style="width: 80%;">
					</td>
				</tr>
				<tr>
					<td>父类菜单： <input id="parentId" class="easyui-combobox"
						name="language" style="width: 80%;"
						data-options="
                    url:'/menu/getMenuList',
                    method:'get',
                    valueField:'id',
                    textField:'text',
                    panelHeight:'auto',
                    labelPosition: 'top',
                    onChange:selectMenu
                    
                    ">
					</td>
				</tr>


				<tr>
					<td>权限选择： <select class="easyui-combogrid" id="permission"
						style="width: 80%"
						data-options="
                    panelWidth: 260,
                    multiple: true,
                    idField: 'id',
                    textField: 'text',
                    url: '/menu/getPermissionByMenuId/-1',
                    method: 'get',
                    columns: [[
                        {field:'id',title:'权限ID',hidden:true,width:80},
                        {field:'text',title:'权限名称',width:120}
                    ]],
                    fitColumns: true,
                    labelPosition: 'left'
                ">
					</select>
					</td>
				</tr>
				<tr align="center">
					<td><a href="javascript:void(0)" class="easyui-linkbutton"
						onclick="submitAddRole()">--确认添加--</a></td>
				</tr>
			</table>
		</div>
	</div>
	<table id="RoleTable" title="角色管理" class="easyui-treegrid"
		style="width: 90%;"
		data-options="
                url: '/gm-role/getGmRole',
                method: 'get',
                rownumbers: true,
                pagination: true,
                pageSize: 30,
                pageList: [30,60,90],
                idField: 'uuid',
                treeField: 'text',
                fitColumns:true,
                scrollbarSize:0,
                toolbar:toolbar
                
            ">
		<thead>
			<tr>
				<th data-options="field:'parentId', hidden:true">父菜单ID</th>
				<th data-options="field:'id', hidden:true">ID</th>
				<th
					data-options="field:'text',align:'center',halign:'center',width:220">角色名称</th>
				<th
					data-options="field:'role',width:300,align:'center',halign:'center'">是否为角色</th>
				<th
					data-options="field:'menu',width:300,align:'center',halign:'center'">是否为菜单</th>
				<th
					data-options="field:'href',width:300,align:'left',halign:'center'">请求路径</th>
			</tr>
		</thead>
	</table>

	
	<script>
		var toolbar = [ {
			text : '添加角色',
			iconCls : 'icon-add',
			handler : function() {
				$("#roleId").val(null);
				$("#addRoleDialog").dialog("open");
			}
		}, '-', {
			text : '删除角色',
			iconCls : 'icon-remove',
			handler : deleteRole
		}, '-', {
			text : '修改角色权限',
			iconCls : 'icon-edit',
			handler : updateRole
		} ];
		function selectMenu(newValue, oldValue) {
			$('#permission').combogrid("grid").datagrid('reload',
					'/menu/getPermissionByMenuId/' + newValue);
		}
		function updateRole() {
			var row = $("#RoleTable").datagrid("getSelected");
			
			if(row == null || !row.role){
				$.messager.alert('提示', '请选择需要更新的角色行', 'error');
				return;
			}
			$("#roleId").val(row.id);
			$("#name").textbox("setValue",row.text);
			$("#addRoleDialog").dialog("open");
		}
		function deleteRole() {
			var row = $("#RoleTable").datagrid("getSelected");
			if (row == null) {
				$.messager.alert('提示', '请选择要删除的角色行', 'error');
				return;
			}
			if(!row.role){
				$.messager.alert('提示', '请选择要删除的角色行', 'error');
				return;
			}
			$.messager.confirm('警告', '确定要删除' + row.text + '角色吗？', function(r) {
				if (r) {
					var id = row.id;
				
					postAjax("/gm-role/deleteGmRole/" + id, {}, function(data) {
						if (data.code == 0) {
							$("#RoleTable").treegrid('reload');
							$.messager.alert('提示', '删除权限成功');
						} else {
							$.messager.alert('提示', '服务器错误，请联系开发者:' + data.msg,
									'error');
						}
					});
				}
			});

		}
		function submitAddRole() {
			var parentId = $("#parentId").combobox("getValue");
			if (parentId == "-1") {
				$.messager.alert('提示', '请选择父类菜单,否则不能添加角色', 'error');
				return;
			}
			var name = $("#name").val();
			if (name == null || name == "") {
				$.messager.alert('提示', '角名称不能为空', 'error');
				return;
			}
			var selectRow = $("#permission").combogrid("grid").datagrid(
					"getSelections");

			var permissionIds = new Array();
			for ( var r in selectRow) {
				permissionIds.push(selectRow[r].id);
			}
			var roleId = $("#roleId").val();
			var json = {
				"roleId": roleId,
				"menuId" : parentId,
				"permissionIds" : permissionIds,
				"name" : name
			};
			var url = "/gm-role/addGmRole";
			postAjax(url, json,
					function(data) {
						if (data.code == 0) {
							$("#addRoleDialog").dialog("close");
							$("#RoleTable").treegrid('reload');//大括号内是需要的参数
							$.messager.alert('提示', '添加角色成功');
						} else {
							$.messager.alert('提示', '服务器错误，请联系开发者:' + data.msg,
									'error');
						}
					});
		}
	</script>
</body>
</html>